<?php

namespace app\api\controller;

use app\admin\model\product\Argb;
use app\admin\model\product\Attr;
use app\admin\model\product\Brand;
use app\admin\model\product\Casefan;
use app\admin\model\product\Chassis;
use app\admin\model\product\Chassislight;
use app\admin\model\product\Cpu;
use app\admin\model\product\Display;
use app\admin\model\product\Earphone;
use app\admin\model\product\Gpuholder;
use app\admin\model\product\Gpuprolong;
use app\admin\model\product\Graphics;
use app\admin\model\product\Guizhi;
use app\admin\model\product\Handle;
use app\admin\model\product\Harddisk;
use app\admin\model\product\Heat;
use app\admin\model\product\Keyboard;
use app\admin\model\product\Mainboard;
use app\admin\model\product\Memory;
use app\admin\model\product\Mouse;
use app\admin\model\product\Networkcard;
use app\admin\model\product\Power;
use app\admin\model\product\Suit;
use app\common\controller\Api;
use app\common\model\ProductPriceRecord;

/**
 * 配置接口
 */
class Product extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    //产品属性
    public function attr()
    {
        $deploy_type = input('deploy_type',1);

        $list = Attr::where(['deploy_type'=>$deploy_type])->order('weigh desc')->select();

        $this->success('',$list);
    }

    //品牌
    public function brand()
    {
        $deploy_type = input('deploy_type',1);

        $list = Brand::where(['deploy_type'=>$deploy_type])->order('weigh desc')->select();

        $this->success('',$list);
    }


    //产品列表
    public function getGoodsList()
    {
        $deploy_type = input('deploy_type',1);

        $keyword = input('title','');

        $attr_id = input('attr_id',0);

        $brand_id = input('brand_id',0);

        $ishiden = input('ishiden',0);

        $sort = input('sort','');

        //$cpu_gongdian = input('cpu_gongdian','');
        $cpu_jiekou = input('cpu_jiekou','');
        $gonghao = input('gonghao',0);
        //$neicun_daishu = input('neicun_daishu','');
        $zhuban_banxing = input('zhuban_banxing','');
        $zhuban_ddr = input('zhuban_ddr','');
        //$zhuban_gongdian = input('zhuban_gongdian','');
        $zhuban_jiekou = input('zhuban_jiekou','');
        //$zhuban_pinlv = input('zhuban_pinlv','');

        if ($sort == '1'){
            $orderBy = 'price desc';
        } elseif ($sort == '2'){
            $orderBy = 'price asc';
        } else {
            $orderBy = 'createtime desc';
        }

        $where = ['deploy_type'=>$deploy_type];

        if ($keyword){
            $where['title'] = ['like', '%' . $keyword . '%'];
        }
        if ($attr_id){
            $where['attr_id'] = $attr_id;
        }
        if ($brand_id){
            $where['brand_id'] = $brand_id;
        }
        $list = \app\admin\model\product\Product::where($where)
            ->order($orderBy)
            ->paginate(20, false, ['query' => request()->get()]);
        foreach ($list as $k=>$v){
            $check_jrx = [

            ];

            //配置类型,1cpu,2主板,3显卡,4内存,5硬盘,6电源,7散热,8机箱,9显示器,10配件,11外设
            switch ($deploy_type) {
                case '1':
                    $son = Cpu::get(['product_id' => $v['id']]);
                    if ($zhuban_jiekou&&$son){
                        //dump($zhuban_jiekou);
                        //dump($son['jiekou']);
                        if ($zhuban_jiekou==$son['jiekou']){
                            $check_jrx = [
                                'type'=>'success',
                                'msg'=>"CPU和主板接口兼容【'{$zhuban_jiekou}'】"
                            ];
                        } else {
                            $check_jrx = [
                                'type'=>'error',
                                'msg'=>"CPU和主板接口不兼容，CPU【{$son['jiekou']}】，主板【{$zhuban_jiekou}}】"
                            ];
                        }
                    }
                    break;
                case '2':
                    $son = Mainboard::get(['product_id' => $v['id']]);
                    if ($cpu_jiekou&&$son){
                        if ($cpu_jiekou==$son['jiekou']){
                            $check_jrx = [
                                'type'=>'success',
                                'msg'=>"CPU和主板接口兼容【'{$cpu_jiekou}'】"
                            ];
                        } else {
                            $check_jrx = [
                                'type'=>'error',
                                'msg'=>"CPU和主板接口不兼容，CPU【{$cpu_jiekou}】，主板【{$son['jiekou']}}】"
                            ];
                        }
                    }
                    break;
                case '3':
                    $son = Graphics::get(['product_id' => $v['id']]);
                    break;
                case '4':
                    $son = Memory::get(['product_id' => $v['id']]);
                    if ($zhuban_ddr&&$son){
                        if ($zhuban_ddr==$son['daishu']){
                            $check_jrx = [
                                'type'=>'success',
                                'msg'=>"主板和内存1接口兼容，接口【{$zhuban_ddr}】"
                            ];
                        } else {
                            $check_jrx = [
                                'type'=>'error',
                                'msg'=>"主板和内存1接口不兼容，主板【{$zhuban_ddr}】，内存【{$son['daishu']}}】"
                            ];
                        }
                    }
                    break;
                case '5':
                    $son = Harddisk::get(['product_id' => $v['id']]);
                    break;
                case '6':
                    $son = Power::get(['product_id' => $v['id']]);
                    if ($gonghao&&$son){
                        if ($gonghao-150>$son['dygl']){
                            $check_jrx = [
                                'type'=>'success',
                                'msg'=>"电源满足硬件功耗所需，当前总功耗【'{$gonghao}'】，电源功率【'{$son['dygl']}'】"
                            ];
                        } elseif ($gonghao>=$son['dygl']){
                            $check_jrx = [
                                'type'=>'error',
                                'msg'=>"电源功率不足，当前总功耗【'{$gonghao}'】，电源功率【'{$son['dygl']}'】"
                            ];
                        } else {
                            $suggest = $son['dygl']+150;
                            $check_jrx = [
                                'type'=>'warn',
                                'msg'=>"电源功率可能不足，当前总功耗【'{$gonghao}'】，电源功率【'{$son['dygl']}'】，建议电源【'{$suggest}'】"
                            ];
                        }
                    }
                    break;
                case '7':
                    $son = Heat::get(['product_id' => $v['id']]);
                    break;
                case '8':
                    $son = Chassis::get(['product_id' => $v['id']]);
                    if ($zhuban_banxing&&$son){
                        $zhuban_arr = explode(',',$son['zhuban']);
                        //dump($zhuban_banxing);
                        //dump($zhuban_arr);
                        //dump(in_array($zhuban_banxing,$zhuban_arr));die;
                        if (in_array($zhuban_banxing,$zhuban_arr)){
                            $check_jrx = [
                                'type'=>'success',
                                'msg'=>"机箱与主板大小匹配，机箱是【{$son['zhuban']}】，主板是【{$zhuban_banxing}】"
                            ];
                        } else {
                            $check_jrx = [
                                'type'=>'error',
                                'msg'=>"机箱与主板大小不匹配，机箱是【{$son['zhuban']}】，主板是【{$zhuban_banxing}】"
                            ];
                        }
                    }
                    break;
                case '9':
                    $son = Display::get(['product_id' => $v['id']]);
                    break;
                case '10':
                    if ($attr_id==24){
                        //机箱风扇
                        $son = Casefan::get(['product_id' => $v['id']]);
                    } elseif ($attr_id==25){
                        //机箱灯条
                        $son = Chassislight::get(['product_id' => $v['id']]);
                    } elseif ($attr_id==26){
                        //显卡支架
                        $son = Gpuholder::get(['product_id' => $v['id']]);
                    } elseif ($attr_id==27){
                        //显卡延长线
                        $son = Gpuprolong::get(['product_id' => $v['id']]);
                    } elseif ($attr_id==28){
                        //ARGB集线器
                        $son = Argb::get(['product_id' => $v['id']]);
                    } elseif ($attr_id==29){
                        //网卡
                        $son = Networkcard::get(['product_id' => $v['id']]);
                    } elseif ($attr_id==35){
                        //硅脂
                        $son = Guizhi::get(['product_id' => $v['id']]);
                    }
                    break;
                case '11':
                    if ($attr_id==35){
                        //键鼠套装
                        $son = Suit::get(['product_id' => $v['id']]);
                    } elseif ($attr_id==34){
                        //手柄
                        $son = Handle::get(['product_id' => $v['id']]);
                    } elseif ($attr_id==33){
                        //耳机
                        $son = Earphone::get(['product_id' => $v['id']]);
                    } elseif ($attr_id==32){
                        //鼠标
                        $son = Mouse::get(['product_id' => $v['id']]);
                    } elseif ($attr_id==31){
                        //键盘
                        $son = Keyboard::get(['product_id' => $v['id']]);
                    }

                    break;
            }
            if ($ishiden&&$check_jrx['type']=='error'){
                unset($list[$k]);
            } else {
                $list[$k]['son'] = $son;
                $list[$k]['check_jrx'] = $check_jrx;
            }

        }
        //die;
        $this->success('请求成功',$list);

    }

    //产品详情
    public function goodsInfo()
    {
        $deploy_type = input('deploy_type',1);

        $attr_id = input('attr_id',0);

        $product_id = input('product_id',0);

        $info = \app\api\model\product\Product::get($product_id);

        switch ($deploy_type) {
            case '1':
                $son = Cpu::get(['product_id' => $info['id']]);

                break;
            case '2':
                $son = Mainboard::get(['product_id' => $info['id']]);

                break;
            case '3':
                $son = Graphics::get(['product_id' => $info['id']]);
                break;
            case '4':
                $son = Memory::get(['product_id' => $info['id']]);

                break;
            case '5':
                $son = Harddisk::get(['product_id' => $info['id']]);
                break;
            case '6':
                $son = Power::get(['product_id' => $info['id']]);

                break;
            case '7':
                $son = Heat::get(['product_id' => $info['id']]);
                break;
            case '8':
                $son = Chassis::get(['product_id' => $info['id']]);
                break;
            case '9':
                $son = Display::get(['product_id' => $info['id']]);
                break;
            case '10':
                if ($attr_id==24){
                    //机箱风扇
                    $son = Casefan::get(['product_id' => $info['id']]);
                } elseif ($attr_id==25){
                    //机箱灯条
                    $son = Chassislight::get(['product_id' => $info['id']]);
                } elseif ($attr_id==26){
                    //显卡支架
                    $son = Gpuholder::get(['product_id' => $info['id']]);
                } elseif ($attr_id==27){
                    //显卡延长线
                    $son = Gpuprolong::get(['product_id' => $info['id']]);
                } elseif ($attr_id==28){
                    //ARGB集线器
                    $son = Argb::get(['product_id' => $info['id']]);
                } elseif ($attr_id==29){
                    //网卡
                    $son = Networkcard::get(['product_id' => $info['id']]);
                } elseif ($attr_id==35){
                    //硅脂
                    $son = Guizhi::get(['product_id' => $info['id']]);
                }
                break;

            case '11':
                if ($attr_id==35){
                    //键鼠套装
                    $son = Suit::get(['product_id' => $info['id']]);
                } elseif ($attr_id==34){
                    //手柄
                    $son = Handle::get(['product_id' => $info['id']]);
                } elseif ($attr_id==33){
                    //耳机
                    $son = Earphone::get(['product_id' => $info['id']]);
                } elseif ($attr_id==32){
                    //鼠标
                    $son = Mouse::get(['product_id' => $info['id']]);
                } elseif ($attr_id==31){
                    //键盘
                    $son = Keyboard::get(['product_id' => $info['id']]);
                }

                break;
        }

        $info['son'] = $son;

        $this->success('请求成功',$info);
    }

    //子产品
    public function sub()
    {
        $product_id = input('product_id',0);

        $list = \app\admin\model\product\Sub::where(['product_id'=>$product_id])
            ->order('weigh desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('',$list);
    }

    //价格趋势
    public function priceStat()
    {
        $product_id = input('product_id',0);

        $type = input('type',0);
        if ($type==0){
            //全部
            $start_date = '2020-01-01 00:00';

        } elseif ($type==1){
            //180天
            $start_date = date('Y-m-d 00:00', strtotime('-180 days'));
        } elseif ($type==2){
            //60天
            $start_date = date('Y-m-d 00:00', strtotime('-60 days'));
        } elseif ($type==3){
            //30天
            $start_date = date('Y-m-d 00:00', strtotime('-30 days'));
        }
        $end_date = date('Y-m-d 23:59', time());
        $data = array();
        $time_arr = [];
        $day = $this->daysBetweenDates($start_date,$end_date);

        for ($i = 0;$i <= $day;$i++){
            $time = strtotime(date('Y-m-d',strtotime("-".($day-$i)." day")));
            $end_time = $time+86400;
            $price = ProductPriceRecord::where('product_id',$product_id)
                ->whereTime('date', 'between', [date('Y-m-d',$time), date('Y-m-d',$end_time)])
                ->find();

            $data['price'][$i] = $price?$price['price']:0;
            $time_arr[$i] = date('Y-m-d',$time);
        }
        $echart_data = [
            'time'=>$time_arr,
            'data'=>$data
        ];
        $this->success('请求成功',$echart_data);
    }

    public function daysBetweenDates($date1, $date2) {
        // 创建 DateTime 对象
        $datetime1 = new \DateTime($date1);
        $datetime2 = new \DateTime($date2);

        // 计算日期差值
        $interval = $datetime1->diff($datetime2);

        // 返回天数差值
        return $interval->days;
    }
}